class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
inorder = [9,3,15,20,7]

postorder = [9,15,7,20,3]

def dfs(inorder,postorder,inorderIndexs,inorder_left,inorder_right,postorder_left,postorder_right):
    #边界条件
    if postorder_left>postorder_right:
        return None
    #根节点在后序遍历的过程中的索引
    root_post_index=postorder_right
    #根节点在中序遍历过程中的索引位置
    root_in_index=inorderIndexs[postorder[root_post_index]]
    #右子树中的结点数量
    size_in_right=inorder_right-root_in_index

    #构建根节点
    root=TreeNode(postorder[root_post_index])
    #构建右子树
    root.right=dfs(inorder,postorder,inorderIndexs,root_in_index+1,inorder_right,postorder_right-size_in_right,postorder_right-1)
    #构建左子树
    root.left=dfs(inorder,postorder,inorderIndexs,inorder_left,root_in_index-1,postorder_left,postorder_right-size_in_right-1)
    return root
def buildTree(inorder,postorder):
    n=len(inorder)
    inorderIndexs={element: i for i,element in enumerate(inorder)}
    print(inorderIndexs)
    return dfs(inorder,postorder,inorderIndexs,0,n-1,0,n-1)

buildTree(inorder,postorder)